package com.example.apartment.common;

/**
 * 通用返回对象
 *
 * @author wuzexin
 * @date 2021/10/27 20:04
 */
public class ApiRestResponse<T> {
    // 成功状态码
    private static final Integer OK_CODE =  10000;
    // 成功提示信息
    private static final String OK_MSG = "SUCCESS";

    // 状态码
    private Integer status;

    // 提示信息
    private String message;

    // 返回数据
    private T data;

    // 默认构造构造方法为返回成功
    public ApiRestResponse() {
        this(OK_CODE, OK_MSG);
    }

    public ApiRestResponse(Integer status, String message) {
        this.status = status;
        this.message = message;
    }

    public ApiRestResponse(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ApiRestResponse success() {
        return new ApiRestResponse();
    }

    public static <T> ApiRestResponse success(T result) {
        ApiRestResponse<T> apiRestResponse = new ApiRestResponse<>();
        apiRestResponse.setData(result);
        return apiRestResponse;
    }

    public static ApiRestResponse error(RespExceptionEnum e) {
        return new ApiRestResponse(e.getCode(), e.getMsg());
    }

    public static ApiRestResponse error(Integer code, String msg) {
        return new ApiRestResponse(code, msg);
    }

    @Override
    public String toString() {
        return "ApiRestResponse{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
